package modules.majiang.viewManager
{
	import com.netease.protobuf.UInt64;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import br.com.stimuli.loading.loadingtypes.LoadingItem;
	
	import communication.majiang.MajiangMsgEvent;
	
	import modules.ViewManager;
	import modules.gameTop.GameTopManager;
	import modules.load.Load;
	import modules.load.LoadEvent;
	import modules.lobby.Lobby;
	import modules.lobby.LobbyEvent;
	import modules.majiang.Majiang;
	import modules.majiang.MajiangEvent;
	import modules.majiang.data.MatchState;
	import modules.majiang.data.TableDataModel;
	import modules.majiang.data.TablesDataManager;
	import modules.majiang.effect.ChipsFromPotEffectData;
	import modules.majiang.effect.ChipsToPotEffectData;
	import modules.manager.SoundManager;
	
	import protobuf.MSPKG_DICE_NTF;
	import protobuf.MSPKG_DIST_TILES_NTF;

	public class MajiangUIManager extends ViewManager
	{
		private static var _loading:Boolean = false;

		private static var isLoad:Boolean = false;

		private var isInit:Boolean = false;

		private var isShow:Boolean = false;

		private static var loadingPaths:Array;

		public var tableId:UInt64

		public var roomIndex:int; //缩小窗口后的房间序号

		private var seatManager:SeatManager;

		private var playerViewManager:PlayerViewManager;

		private var dealerViewManager:DealerViewManager;

		private var handCardManager:HandCardManager;

		private var ownCardsViewManager:OwnCardsViewManager;

		private var actionViewManager:ActionViewManager;

		private var betViewManager:BetViewManager;

		private var cardPotViewManager:CardPotViewManager;

		private var bonusPotViewManager:BonusPotViewManager;

		private var countdownViewManager:CountdownViewManager;

		public function get loading():Boolean
		{
			return _loading;
		}

		public function set loading(value:Boolean):void
		{
			_loading = value;
			if (_loading)
				dispatcher.addEventListener(LoadEvent.LOAD_SINGLE_COMPLETE, onLoadSingleComplete);
			else
				dispatcher.removeEventListener(LoadEvent.LOAD_SINGLE_COMPLETE, onLoadSingleComplete);

		}

		public function MajiangUIManager(tableId:UInt64)
		{
			this.tableId = tableId;

		}

		override protected function init():void
		{
			if (loading)
				return;

			if (!isLoad)
			{
				loading = true;
				loadingPaths = [Majiang.viewPath];
				dispatcher.dispatchEvent(new LoadEvent(LoadEvent.LOAD_RESOURCE, {urls: loadingPaths}));
				return;
			}

			mainUI = Load.getInstance("fla.view.majiang.TableView");

			mainUI.countDown_txt.visible = false;

			mainUI.dice_mc.visible = false;
			mainUI.dice_mc.gotoAndStop(1);
			mainUI.diceValue_mc.visible = false;
			mainUI.diceValue_mc.gotoAndStop(1);

			mainUI.fetch_2.visible = false;
			mainUI.fetch_3.visible = false;
			mainUI.fetch_4.visible = false;
			mainUI.fetch_5.visible = false;
			mainUI.fetch_6.visible = false;
			for (var i:int = 1; i <= 6; i++)
			{
				var fetchCard:* = mainUI["fetch_" + i];
				fetchCard.visible = false;
				var cardBitmap:Bitmap = new Bitmap();
				cardBitmap.name = "cardBitmap";
				fetchCard.addChild(cardBitmap);

				var disCard:* = mainUI["discard_" + i];
				disCard.visible = false;
				cardBitmap = new Bitmap();
				cardBitmap.name = "cardBitmap";
				disCard.addChild(cardBitmap);

				var chip:DisplayObject = mainUI["chip_" + i];
				chip.visible = false;
				ChipsToPotEffectData.startPoints[i] = new Point(chip.x, chip.y);
				ChipsFromPotEffectData.endPoints[i] = new Point(chip.x, chip.y);
			}

			seatManager = new SeatManager(tableId, mainUI);

			playerViewManager = new PlayerViewManager(tableId, mainUI);

			dealerViewManager = new DealerViewManager(tableId, mainUI);

			handCardManager = new HandCardManager(tableId, mainUI);

			ownCardsViewManager = new OwnCardsViewManager(tableId, mainUI);

			actionViewManager = new ActionViewManager(tableId, mainUI);

			betViewManager = new BetViewManager(tableId, mainUI);

			cardPotViewManager = new CardPotViewManager(tableId, mainUI.cardpot_mc);

			bonusPotViewManager = new BonusPotViewManager(tableId, mainUI);

			countdownViewManager = new CountdownViewManager(tableId, mainUI);

			Majiang.initCashierViewManager();

			GameTopManager.init();

			isInit = true;

			mainUI.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			mainUI.removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);

			show();
		}

		public function show():void
		{
			if (!isInit)
			{
				init();
				return;
			}

			logger("显示麻将游戏界面");

			UIAllRefer.contentLayer.addChild(mainUI);

			updateView();

			isShow = true;

			//更新麻将桌子界面中显示
			if (tableDataModel)
				dispatcher.dispatchEvent(new MajiangEvent(MajiangEvent.UPDATE_MAJIANG_TABLE_VIEW, {tableId: tableId}));

			dispatcher.dispatchEvent(new LobbyEvent(LobbyEvent.LOBBY_CLOSE));
		}

		override protected function updateView():void
		{
			// TODO Auto Generated method stub
			super.updateView();
		}

		public function close():void
		{
			logger("关闭麻将界面");

			if (mainUI && mainUI.parent)
				mainUI.parent.removeChild(mainUI);

			Lobby.isShow = false;

			dispatcher.dispatchEvent(new LobbyEvent(LobbyEvent.LOBBY_SHOW));
		}

		public function get tableDataModel():TableDataModel
		{
			return TablesDataManager.tableDataModeDic[tableId.toString()];
		}

		override protected function onAddToStage(event:Event):void
		{
			mainUI.addEventListener(MouseEvent.CLICK, onClick);

			dispatcher.addEventListener(MajiangEvent.UPDATE_MAJIANG_TABLE_VIEW, onUpdateTableView);

			dispatcher.addEventListener(MajiangEvent.UPDATE_COUNTDOWN_NUMBER, onUpdateCountdownNumber);

			//开始摇色子
			dispatcher.addEventListener(MajiangEvent.WAVE_PARTICLE_START, onWaveParticleStart);

			//开始发牌
			dispatcher.addEventListener(MSProto.MSID_DIST_TILES_NTF.toString(), onDistTilesNtf);

			dispatcher.addEventListener(MajiangEvent.GET_FETCH_CARD, onGetFetchCard);

			dispatcher.addEventListener(MajiangEvent.GET_DISCARD, onGetDisCard);

			dispatcher.addEventListener(MajiangEvent.GET_FLY_CHIP, onGetFlyChip);
		}

		override protected function onRemovedFromStage(event:Event):void
		{
			mainUI.removeEventListener(MouseEvent.CLICK, onClick);

			dispatcher.removeEventListener(MajiangEvent.UPDATE_MAJIANG_TABLE_VIEW, onUpdateTableView);

			dispatcher.removeEventListener(MajiangEvent.UPDATE_COUNTDOWN_NUMBER, onUpdateCountdownNumber);

			//开始摇色子
			dispatcher.removeEventListener(MajiangEvent.WAVE_PARTICLE_START, onWaveParticleStart);

			//开始发牌
			dispatcher.removeEventListener(MSProto.MSID_DIST_TILES_NTF.toString(), onDistTilesNtf);

			dispatcher.removeEventListener(MajiangEvent.GET_FETCH_CARD, onGetFetchCard);

			dispatcher.removeEventListener(MajiangEvent.GET_DISCARD, onGetDisCard);

			dispatcher.removeEventListener(MajiangEvent.GET_FLY_CHIP, onGetFlyChip);
		}

		private function onClick(event:MouseEvent):void
		{
			switch (event.target)
			{
				case mainUI.leave_btn:
					dispatcher.dispatchEvent(new MajiangMsgEvent(MSProto.MSID_LEAVE_TABLE_REQ.toString(), {tableId: tableId}));
					break;
			}
		}

		private function onUpdateTableView(event:MajiangEvent):void
		{
			if (tableId.toString() != event.data.tableId.toString())
				return;
		}

		private function onUpdateCountdownNumber(event:MajiangEvent):void
		{
			mainUI.countDown_txt.visible = event.data.number > 0;
			mainUI.countDown_txt.text = event.data.number;
		}

		private function onWaveParticleStart(event:MajiangEvent):void
		{
			var recvDiceNtf:MSPKG_DICE_NTF = event.data;
			if (tableId.toString() != recvDiceNtf.tableId.toString())
				return;

			var dice:uint = 1;
			dice = recvDiceNtf.dice;

			//修改比赛状态 
			tableDataModel.setMatchState(MatchState.WAVE_PARTICLE);

			mainUI.diceValue_mc.gotoAndStop(dice);
			//摇色子
			mainUI.dice_mc.visible = true;
			mainUI.dice_mc.gotoAndPlay(1);
			SoundManager.instance.diceSoundPlay();
			mainUI.dice_mc.addEventListener(Event.EXIT_FRAME, oncExitFrame);
		}

		private function oncExitFrame(event:Event):void
		{
			if (mainUI.dice_mc.currentFrame == mainUI.dice_mc.totalFrames)
			{
				mainUI.dice_mc.stop();
				mainUI.dice_mc.visible = false;
				mainUI.dice_mc.removeEventListener(Event.EXIT_FRAME, oncExitFrame);

				mainUI.diceValue_mc.visible = true;
			}
		}

		private function onDistTilesNtf(event:MajiangMsgEvent):void
		{
			var pkg:MSPKG_DIST_TILES_NTF = event.data;
			if (pkg.tableId.toString() != tableId.toString())
				return;
			mainUI.diceValue_mc.visible = false;
		}

		private function onGetFetchCard(event:MajiangEvent):void
		{
			var position:int = 0;
			if (event.data.position)
			{
				position = event.data.position;
			}
			if (mainUI.hasOwnProperty("fetch_" + position))
			{
				event.data.card = mainUI["fetch_" + position];
			}
		}

		private function onGetDisCard(event:MajiangEvent):void
		{
			var position:int = 0;
			if (event.data.position)
			{
				position = event.data.position;
			}
			if (mainUI.hasOwnProperty("discard_" + position))
			{
				event.data.card = mainUI["discard_" + position];
			}
		}

		private function onGetFlyChip(event:MajiangEvent):void
		{
			var position:int = 0;
			if (event.data.position)
			{
				position = event.data.position;
			}
			if (mainUI.hasOwnProperty("chip_" + position))
			{
				event.data.chip = mainUI["chip_" + position];
			}
		}

		public function get mainView():MovieClip
		{
			return mainUI;
		}

		public function get visible():Boolean
		{
			return mainUI.parent && mainUI.stage && mainUI.visible;
		}

		public function set mask(value):void
		{
			mainUI.mask = value;
		}

		public function get parent():DisplayObjectContainer
		{
			return mainUI.parent;
		}

		private function onLoadSingleComplete(event:LoadEvent):void
		{
			if (loadingPaths.length == 0)
				return;
			var loadingItem:LoadingItem = event.data;
			var index:int = loadingPaths.indexOf(loadingItem.url.url);
			if (index != -1)
			{
				loadingPaths.splice(index, 1);
				if (loadingPaths.length == 0)
				{
					loading = false;
					isLoad = true;
					init();
				}
			}
		}
	}
}
